package com.baize.baizeapp.test.sku

import android.app.Activity
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.baize.baizeapp.R
import com.baize.baizeapp.utils.dip2px
import com.baize.baizeapp.utils.getHeightInPx
import com.baize.baizeapp.utils.getWidthInPx
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog

class ProductSkuInfoDialogUtils {
    var bottomSheetDialog: BottomSheetDialog? = null
    var layoutView: View? = null


    companion object {
        fun build(mContext: Activity): Builder {
            return Builder(mContext)
        }
    }

    class Builder(val activity: Activity) {
        var minHeight: Float = 0.5f//最小高度百分比
        var myStyle = R.style.bottomSheetDialog
        var bottomSheetDialogUtils = ProductSkuInfoDialogUtils()
        var bottomSheetBehavior: BottomSheetBehavior<View>? = null
        var productInfoList: MutableList<Pair<String, String>>? = null
        var productInfoTitle : String? = null
        var listener: (() -> Unit)? = null

        fun setProductInfoList(infoList: MutableList<Pair<String, String>>?): ProductSkuInfoDialogUtils.Builder {
            this.productInfoList = infoList
            return this
        }

        fun setConfirmClickListener(listener: (() -> Unit)?): ProductSkuInfoDialogUtils.Builder {
            this.listener = listener
            return this
        }

        fun setProductInfoTitle(title: String): ProductSkuInfoDialogUtils.Builder {
            this.productInfoTitle = title
            return this
        }

        /**
         * 设置布局
         */
        fun setView(layoutId: Int): ProductSkuInfoDialogUtils.Builder {
            bottomSheetDialogUtils.layoutView =
                LayoutInflater.from(activity).inflate(layoutId, null)
            return this
        }

        fun setStyle(myStyle: Int): ProductSkuInfoDialogUtils.Builder {
            this.myStyle = myStyle
            return this
        }

        fun setMinHeight(minHeight: Float): ProductSkuInfoDialogUtils.Builder {
            this.minHeight = minHeight
            return this
        }

        private fun updateBottomView(
            liveProductToBuy: TextView?,
            text: String,
            isEnable: Boolean
        ) {
            liveProductToBuy?.text = text
            liveProductToBuy?.isEnabled = isEnable
        }

        fun show(): ProductSkuInfoDialogUtils {
            bottomSheetDialogUtils.run {
                if (layoutView == null) {
                    setView(R.layout.live_product_shopping_bottom_sheet)
                }
                bottomSheetDialog = BottomSheetDialog(activity, myStyle)
                val productInfoRoot = layoutView?.findViewById<LinearLayout>(R.id.live_product_style)
                productInfoRoot?.removeAllViews()
                productInfoList?.forEachIndexed { index,it->
//                    val itemView = View.inflate(activity, R.layout.business_product_info_item, null)
//                    val productInfoKey = itemView?.findViewById<HSTextView>(R.id.product_info_key)
//                    val productInfoValue = itemView?.findViewById<HSTextView>(R.id.product_info_value)
//                    productInfoKey?.text = it.first
//                    productInfoValue?.text = it.second
                    productInfoRoot?.addView(TextView(activity))
                }
                Log.i("yanze_dialog","productInfoRoot高度：${productInfoRoot?.height} ${productInfoRoot?.measuredHeight}")
                //限制BottomDialog高度
                val widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(
                    activity.getWidthInPx() - activity.dip2px(30f),
                    View.MeasureSpec.AT_MOST
                )
                val heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(
                    0,
                    View.MeasureSpec.UNSPECIFIED
                )
                productInfoRoot!!.measure(widthMeasureSpec, heightMeasureSpec)
                Log.i("yanze_dialog","productInfoRoot高度 measure后：${productInfoRoot?.height} ${productInfoRoot?.measuredHeight}")
                val rootViewHeight = productInfoRoot.measuredHeight
                layoutView!!.measure(widthMeasureSpec, heightMeasureSpec)
                var measuredHeight = layoutView!!.measuredHeight + rootViewHeight
                Log.i("yanze_dialog","measuredHeight：${measuredHeight} = ${layoutView!!.measuredHeight} + ${productInfoRoot.measuredHeight}")
                val maxHeight = activity.getHeightInPx()
                Log.i("yanze_dialog","max高度：${maxHeight}")
                if (measuredHeight > maxHeight) {
                    measuredHeight = maxHeight
                }
                val minHeightData = (activity.getHeightInPx() * minHeight).toInt()
                Log.i("yanze_dialog","min高度：${minHeightData}")
                if (measuredHeight < minHeightData) {
                    measuredHeight = minHeightData
                }
                bottomSheetDialog?.setContentView(
                    layoutView!!,
                    ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, measuredHeight)
                )
                bottomSheetBehavior = BottomSheetBehavior.from(layoutView?.parent as View)
                bottomSheetBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
                bottomSheetBehavior?.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
                    override fun onStateChanged(bottomSheet: View, newState: Int) {
                        if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                            bottomSheetDialog?.dismiss()
                            bottomSheetBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
                        }
                    }

                    override fun onSlide(bottomSheet: View, slideOffset: Float) {
                    }

                })
                //设置透明背景
                bottomSheetDialog?.window?.findViewById<View>(R.id.design_bottom_sheet)
                    ?.setBackgroundResource(android.R.color.transparent)
                activity.window.setWindowAnimations(myStyle)//设置动画效果
                bottomSheetDialog?.show()
            }
            return bottomSheetDialogUtils
        }

    }

}